package com.zito.health.controller;

import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.dto.LaboratoryExamineDto;
import com.zito.health.entity.Laboratory;
import com.zito.health.entity.LaboratoryCredentials;
import com.zito.health.entity.LaboratoryExamine;
import com.zito.health.entity.User;
import com.zito.health.service.LaboratoryCredentialsService;
import com.zito.health.service.LaboratoryExamineService;
import com.zito.health.service.LaboratoryService;
import com.zito.health.util.CacheUtil;
import com.zito.health.vo.LaboratoryExamineVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: gxt
 * @time: 2019/11/18 16:12
 */
@Slf4j
@ResponseResult
@RestController
@RequestMapping("/laboratoryExamine")
public class LaboratoryExamineController {

    @Autowired
    private LaboratoryExamineService laboratoryExamineService;
    @Autowired
    private LaboratoryService laboratoryService;
    @Autowired
    private LaboratoryCredentialsService laboratoryCredentialsService;
    /**
     * 实验室审核，
     * @param laboratoryExamineDto
     * @return
     */
    @PostMapping()
    @Transactional
    public VerifyResult examine(LaboratoryExamineDto laboratoryExamineDto){
        VerifyResult verifyResult = new VerifyResult();
        if(laboratoryExamineDto.getLaboratoryId()==null) {
            verifyResult.setResult(false);
            verifyResult.setMsg("无laboratoryId");
            return verifyResult;
        }

        LaboratoryExamine model=new LaboratoryExamine();
        model.setLaboratoryId(laboratoryExamineDto.getLaboratoryId());
        model=laboratoryExamineService.queryOne(model);

        LaboratoryExamine laboratoryExamine=new LaboratoryExamine();
        BeanUtils.copyProperties(laboratoryExamineDto, laboratoryExamine);

        if(model==null){
            laboratoryExamineService.save(laboratoryExamine);
        }else{
            laboratoryExamineService.update(laboratoryExamine);
        }

        Laboratory laboratory=new Laboratory();
        laboratory.setStates(laboratoryExamineDto.getLaboratoryStates());
        laboratory.setId(laboratoryExamineDto.getLaboratoryId());

        if(laboratoryExamineDto.getLaboratoryStates() == 4){//市局通过，要插入证书信息
            LaboratoryCredentials laboratoryCredentials =new LaboratoryCredentials();
            laboratoryCredentials.setCertificate(laboratoryExamineDto.getCertificate());
            laboratoryCredentials.setStartdate(laboratoryExamineDto.getStartdate());
            laboratoryCredentials.setValidity(laboratoryExamineDto.getValidity());
            laboratoryCredentials.setUnitId(CacheUtil.getCacheUser().getUser().getUnitId());
            laboratoryCredentials.setLaboratoryId(laboratoryExamineDto.getLaboratoryId());
            laboratoryCredentials.setRecords("");
            laboratoryCredentialsService.save(laboratoryCredentials);
        }


        laboratoryService.update(laboratory);


        return verifyResult;
    }

    @GetMapping("/queryByLaboratoryId")
    public LaboratoryExamine queryByLaboratoryId(Integer laboratoryId){
        LaboratoryExamine param = new LaboratoryExamine();
        param.setLaboratoryId(laboratoryId);
        LaboratoryExamine laboratoryExamine = laboratoryExamineService.queryOne(param);
        return laboratoryExamine;
    }

    /**
     * 审核首页
     * @auth sun
     */
    @GetMapping("/queryAuditHome")
    public LaboratoryExamineVo queryAuditHome(Integer laboratoryId){
        LaboratoryExamineVo laboratoryExamineVo = laboratoryExamineService.queryAuditHome(laboratoryId);
        return laboratoryExamineVo;
    }
}
